package com.neusoft.bizcore.notice.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.notice.system.dto.SystemNoticeDTO;
import com.neusoft.bizcore.notice.system.service.SystemNoticeService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "消息中心")
@RestController
@RequestMapping(value = "/api/w/notification")
public class SystemNoticeController {
    @Autowired
    private SystemNoticeService systemNoticeService;

    @ApiOperation("查询所有系统通知")
    @GetMapping
    public ResultListDTO<SystemNoticeDTO> index(final Searchable searchable) {
        final List<SystemNoticeDTO> DTOs = this.systemNoticeService.findAll(searchable);
        final ResultListDTO<SystemNoticeDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有系统通知")
    @GetMapping("/p")
    public PageResultDTO<SystemNoticeDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        return PageResultDTO.success(this.systemNoticeService.search(pageable, searchable, user));
    }

    @ApiOperation(value = "修改系统通知")
    @PutMapping("/{id}")
    public ResultDTO<SystemNoticeDTO> update(@PathVariable final Long id,
            @RequestBody final SystemNoticeDTO dto) {
        final SystemNoticeDTO result = this.systemNoticeService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("查看系统通知详情")
    @GetMapping("/{id}")
    public ResultDTO<SystemNoticeDTO> details(@PathVariable final Long id) {
        final SystemNoticeDTO dto = this.systemNoticeService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除系统通知")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.systemNoticeService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("获取当前用户所有未读通知")
    @GetMapping("/current/unread")
    public ResultDTO<Integer> getCurrentUserUnreadMsg(final UserBean user) {
        return ResultDTO.success(this.systemNoticeService.getCurrentUnRead(user));
    }

    @ApiOperation("获取当前所有系统通知的类别")
    @GetMapping("/noticeType")
    public ResultListDTO<String> getNoticeType() {
        return ResultListDTO.success(this.systemNoticeService.findDistinctNoticeType());
    }

    @ApiOperation("批量变更已读")
    @PutMapping("/batchRead")
    public ResultDTO<Void> batchRead(@RequestBody final List<Long> ids) {
        this.systemNoticeService.batchRead(ids);
        return ResultDTO.success();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch/{ids}")
    public ResultDTO<Void> batchDelete(@PathVariable final List<Long> ids) {
        this.systemNoticeService.batchDelete(ids);
        return ResultDTO.success();
    }

}
